package hu.imind.phonecalc.client.dto;

import java.util.Date;

import com.extjs.gxt.ui.client.data.BaseModel;

@SuppressWarnings("serial")
public class Call extends BaseModel {
    // TODO these are not used, check for good practice
    /*
    private String num;
    private String name;
    private Date date;
    private int duration;
    private float price;
    private float total;
    private String cat;
    */

    public Call() {
    }

    @SuppressWarnings("deprecation")
    public Call(String num, String name, String date, String duration, String price, String total, String cat) {
        price = price.replaceAll(" ", "");
        total = total.replaceAll(" ", "");
        set("cat", cat);
        set("name", name);
        set("num", num);
        try{
            String[] s1 = date.split("\\s+");
            String[] s2 = s1[0].split("\\.");
            String[] s3 = s1[1].split(":");
            set("date", new Date(Integer.parseInt(s2[0]), 
                    Integer.parseInt(s2[1]), 
                    Integer.parseInt(s2[2]), 
                    Integer.parseInt(s3[0]), 
                    Integer.parseInt(s3[1]), 
                    Integer.parseInt(s3[2])
            ));
        }catch(Exception e) {
            System.err.println("Date exception: "+e.getMessage() + " szoveg: " + date);
        }
        try{
            duration = duration.replace(" ", "");
            String[] s = duration.split(":");
            set("duration", Integer.parseInt(s[0]) * 60 * 60 + Integer.parseInt(s[1]) * 60 + Integer.parseInt(s[2]));
        }catch(Exception e) {
            System.err.println("Duration exception: " + e.getMessage());
        }
        try{
            if (price.equals("")) {
                set("price", 0);
            } else {
                set("price", Float.parseFloat(price.replaceAll(",", ".")));
            }
        }catch(Exception e) {
            System.err.print("Float exception: " + e.getMessage());
        }
        try{
            if (total.equals("")) {
                set("total", 0);
            } else {
                set("total", Float.parseFloat(total.replaceAll(",", ".")));
            }
        }catch(Exception e) {
            System.err.println("Float exception: " + e.getMessage());
        }
    }
    public Call(String num, Date date, int duration, float price, float total) {
        set("num", num);
        set("date", date);
        set("duration", duration);
        set("price", price);
        set("total", total);
    }

    public String getNum() {
        return get("num");
    }

    public void setNum(String num) {
        set("num", num);
    }

    public String getName() {
        return get("name");
    }

    public void setName(String name) {
        set("name", name);
    }

    public Date getDate() {
        return get("date");
    }

    public void setDate(Date date) {
        set("date", date);
    }

    public int getDuration() {
        return get("duration");
    }

    public void setDuration(int duration) {
        set("duration", duration);
    }

    public float getPrice() {
        return get("price");
    }

    public void setPrice(float price) {
        set("price", price);
    }

    public float getTotal() {
        try{
            return Float.parseFloat(get("total").toString());
        }catch (Exception e) {
            return (float)0;
        }
    }

    public void setTotal(float total) {
        set("total", total);
    }

    public String getCat() {
        try{
            return get("cat");
        }catch (Exception e) {
            return "";
        }
    }

    public void setCat(String cat) {
        set("cat", cat);
    }
}
